<template>
  <div class="addacc">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>修改密码</span>
      </div>
      <el-form
        :model="passwordFrom"
        status-icon
        :rules="rules"
        ref="passwordFrom"
        label-width="100px"
        class="demo-passwordFrom"
      >
        <el-form-item label="原密码" prop="oldPass">
          <el-input
            type="password"
            v-model="passwordFrom.oldPass"
            autocomplete="off"
            show-password
            placeholder="请输入原密码"
          ></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="pass">
          <el-input
            type="password"
            v-model="passwordFrom.pass"
            show-password
            autocomplete="off"
            placeholder="请输入新密码"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input
            v-model.number="passwordFrom.checkPass"
            show-password
            type="password"
            placeholder="请再次输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">提交</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { userEditpwd, checkOldpwd } from "../../api/user";
export default {
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入原密码"));
      } else if (!/^[a-zA-Z0-9_]{6,12}$/.test(value)) {
        callback(new Error("6-12位的数字、字母、下划线"));
      } else {
        callback();
        this.$refs.passwordFrom.validateField("pass"); //验证表单的某个字段  新密码的字段
      }
    };
    const validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入新密码"));
      } else {
        callback();
        this.$refs.passwordFrom.validateField("checkPass"); //验证表单的再次确认字段
        // console.log(value);
      }
    };
    const checkPass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value != this.passwordFrom.pass) {
        callback(new Error("两次输入密码不一致!"));
        console.log(value);
      } else {
        callback();
      }
    };
    return {
      passwordFrom: {
        oldPass: "",
        pass: "",
        checkPass: "",
        group: "",
        region: "",
      },
      rules: {
        oldPass: [{ validator: validatePass, trigger: "blur" }],
        pass: [{ validator: validatePass2, trigger: "blur" }],
        checkPass: [{ validator: checkPass, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm() {
      this.$refs.passwordFrom.validate(async (valid) => {
        if (valid) {
          //格式正确就修改密码
          let res1 = await checkOldpwd({ oldPwd: this.passwordFrom.oldPass });
          console.log(res1);
          let { code, msg } = res1;
          if (code === 200) {
            let res = await userEditpwd({ newPwd: this.passwordFrom.pass });
            console.log(res)
            let { code, msg } = res;
            if (code === 0) {
              this.$message({
                type: "success",
                message: msg,
              });
              localStorage.clear();
              this.$router.push("/login");
            }
          } else {
            this.$message.error(msg);
          }
        }
      });
    },
    resetForm() {
      this.$refs.passwordFrom.resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.addacc {
  background-color: #fff;
  .demo-passwordFrom {
    width: 350px;
  }
}
</style>